﻿#region << 版 本 注 释 >>

/*----------------------------------------------------------------
* 类 名 称 ：ResFactory
* 类 描 述 ：
* 作    者 ：henrychang
* 创建时间 ：2019/1/6 18:49:18
*******************************************************************
* Copyright @ henrychang 2019. All rights reserved.
*******************************************************************
//----------------------------------------------------------------*/

#endregion << 版 本 注 释 >>

using TouchFish.ObjectPool;
using UnityEngine;

namespace TouchFish.AssetLoad
{
    public static class ResFactory
    {
        static ResFactory()
        {
            Debug.Log("Init[ResFactory]");
            //SafeObjectPool<AssetBundleRes>.Instance.MaxCacheCount = 20;
            //SafeObjectPool<AssetRes>.Instance.MaxCacheCount = 40;
            SafeObjectPool<InternalRes>.Instance.MaxCacheCount = 40;
            //SafeObjectPool<NetImageRes>.Instance.MaxCacheCount = 20;
        }

        public static IRes Create(string assetName, string ownerBundleName)
        {
            short assetType = 0;
            if (assetName.StartsWith("Resources/") || assetName.StartsWith("resources://"))
            {
                assetType = ResType.Internal;
            }
            //else if (assetName.StartsWith("NetImage:"))
            //{
            //    assetType = ResType.NetImageRes;
            //}
            //else
            //{
            //    var data = ResDatas.Instance.GetAssetData(assetName, ownerBundleName);

            //    if (data == null)
            //    {
            //        Log.E("Failed to Create Res. Not Find AssetData:" + ownerBundleName + assetName);
            //        return null;
            //    }
            //    else
            //    {
            //        assetType = data.AssetType;
            //    }
            //}

            return Create(assetName, ownerBundleName, assetType);
        }

        public static IRes Create(string assetName, string ownerBundleName, short assetType)
        {
            switch (assetType)
            {
                //case ResType.AssetBundle:
                //    return AssetBundleRes.Allocate(assetName);

                //case ResType.ABAsset:
                //    return AssetRes.Allocate(assetName, ownerBundleName);

                //case ResType.ABScene:
                //    return SceneRes.Allocate(assetName);

                case ResType.Internal:
                    return InternalRes.Allocate(assetName,
                        assetName.StartsWith("resources://")
                            ? InternalResNamePrefixType.Url
                            : InternalResNamePrefixType.Folder);

                //case ResType.NetImageRes:
                //    return NetImageRes.Allocate(assetName);

                //case ResType.LocalImageRes:
                //    return LocalImageRes.Allocate(assetName);

                default:
                    Debug.LogError("Invalid assetType :" + assetType);
                    return null;
            }
        }

        public static IRes Create(string assetName)
        {
            short assetType = 0;
            if (assetName.StartsWith("Resources/") || assetName.StartsWith("resources://"))
            {
                assetType = ResType.Internal;
            }
            //else if (assetName.StartsWith("NetImage:"))
            //{
            //    assetType = ResType.NetImageRes;
            //}
            //else if (assetName.StartsWith("LocalImage:"))
            //{
            //    assetType = ResType.LocalImageRes;
            //}
            //else
            //{
            //    var data = ResDatas.Instance.GetAssetData(assetName);
            //    if (data == null)
            //    {
            //        Log.E("Failed to Create Res. Not Find AssetData:" + assetName);
            //        return null;
            //    }

            //    assetType = data.AssetType;
            //}

            return Create(assetName, assetType);
        }

        /// <summary>
        /// 目前只加载一种资源
        /// </summary>
        /// <param name="assetName"></param>
        /// <param name="assetType"></param>
        /// <returns></returns>
        private static IRes Create(string assetName, short assetType)
        {
            switch (assetType)
            {
                //case ResType.AssetBundle:
                //    return AssetBundleRes.Allocate(assetName);

                //case ResType.ABAsset:
                //    return AssetRes.Allocate(assetName);

                //case ResType.ABScene:
                //    return SceneRes.Allocate(assetName);

                case ResType.Internal:
                    return InternalRes.Allocate(assetName,
                        assetName.StartsWith("resources://")
                            ? InternalResNamePrefixType.Url
                            : InternalResNamePrefixType.Folder);

                //case ResType.NetImageRes:
                //    return NetImageRes.Allocate(assetName);

                //case ResType.LocalImageRes:
                //    return LocalImageRes.Allocate(assetName);

                default:
                    Debug.LogError("Invalid assetType :" + assetType);
                    return null;
            }
        }
    }
}